<?php

namespace App\Http\Controllers;

use App\Models\CustomerSaid;
use App\Models\CustomerService;
use App\Models\SmsLog;
use App\Models\SystemConfig;
use App\Models\SystemConfigGroup;
use App\Models\UserLoginErrorRecord;
use App\Services\ProjectService;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use zgldh\QiniuStorage\QiniuStorage;

class SystemController extends BaseController
{

    /**
     * 客户说
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-11-26
     * @return void
     */
    public function customerSaid()
    {
        return success(CustomerSaid::where('status', 1)->select('id', 'name', 'avatar', 'desc', 'create_time')->orderBy('create_time', 'desc')->whereNull('delete_time')->get());
    }
    /**
     * 获取七牛云token
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-11-26
     * @return void
     */
    public function getQiniuToken()
    {
        $disk = QiniuStorage::disk('qiniu');
        $token = $disk->uploadToken();
        return success(['token' => $token]);
    }
    /**
     * 全局系统配置
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-19
     * @return void
     */
    public function config()
    {
        // 关于我们
        // $about = SystemConfig::where('group_id', 4)->pluck('value', 'key');
        // 联系我们
        $contact = SystemConfig::where('group_id', 5)->pluck('value', 'key');
        // 客服
        // $customer = SystemConfig::where('group_id', 6)->pluck('value', 'key');'customer' => $customer,
        // 友情链接
        $FriendlyLink = SystemConfig::where('group_id', 7)->pluck('value', 'key');
        return success(['contact' => $contact,  'FriendlyLink' => $FriendlyLink]);
    }

    /**
     * 关于我们
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-19
     * @return void
     */
    public function aboutUs()
    {
        $about = SystemConfig::where('group_id', 4)->pluck('value', 'key');
        return success($about);
    }

    /**
     * 首页-服务案例
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-20
     * @return void
     */
    public function homeServiceCase()
    {
        return success(ProjectService::serviceCase());
    }

    /**
     * 服务案例详情
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-26
     * @param Request $request
     * @return void
     */
    public function serviceCaseDetail(Request $request)
    {
        $rules = [
            'id' => ['required'],
        ];

        $message = [
            'id.required' => '缺少参数ID',
        ];

        $payload = $request->only('id');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $result = ProjectService::getProjectDetail($payload['id']);
        // dd($result);
        if ($result) {
            return success($result);
        } else {
            return fail_message();
        }
    }

    /**
     * 检查验证码
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-09-27
     * @param Request $request
     * @return void
     */
    public function checkCode(Request $request)
    {
        $rules = [
            'code' => ['required'],
            'phone' => ['required'],
        ];

        $message = [
            'code.required' => '请输入验证码',
            'phone.required' => '请输入手机号码',
        ];

        $payload = $request->only('code', 'phone');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }
        // 验证验证码
        $verify_code = SmsLog::check_code($payload['code'], $payload['phone']);

        if (empty($verify_code)) {
            return fail_message('验证码不正确');
        }
        return success();
    }

    /**
     * 获取客服列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-12-23
     * @return void
     */
    public function getCustomerService()
    {
        return success(CustomerService::where('status', 1)->whereNull('delete_time')->select('id', 'title', 'img')->orderBy('id', 'desc')->get());
    }


    public function deleteLockUser(Request $request)
    {
        $phone = $request->only('phone');
        return success(UserLoginErrorRecord::where('username', $phone)->delete());
    }
}
